package com.work.music.dao;

import com.work.music.entity.Word;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;
import org.springframework.stereotype.Repository;

@Repository
public interface WordMapper {
    @Delete({
        "delete from music_word",
        "where id = #{id,jdbcType=BIGINT}"
    })
    int deleteByPrimaryKey(Long id);

    @Insert({
        "insert into music_word (targetWord, addtime, ",
        "addip, phonetic, ",
        "chineseWord, voiceFileUrl, ",
        "file)",
        "values (#{targetword,jdbcType=VARCHAR}, #{addtime,jdbcType=TIMESTAMP}, ",
        "#{addip,jdbcType=VARCHAR}, #{phonetic,jdbcType=LONGVARCHAR}, ",
        "#{chineseword,jdbcType=LONGVARCHAR}, #{voicefileurl,jdbcType=LONGVARCHAR}, ",
        "#{file,jdbcType=LONGVARCHAR})"
    })
    @SelectKey(statement="SELECT LAST_INSERT_ID()", keyProperty="id", before=false, resultType=Long.class)
    int insert(Word record);

    @Select({
        "select",
        "id, targetWord, addtime, addip, phonetic, chineseWord, voiceFileUrl, file",
        "from music_word",
        "where id = #{id,jdbcType=BIGINT}"
    })
    @Results({
        @Result(column="id", property="id", jdbcType=JdbcType.BIGINT, id=true),
        @Result(column="targetWord", property="targetword", jdbcType=JdbcType.VARCHAR),
        @Result(column="addtime", property="addtime", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="addip", property="addip", jdbcType=JdbcType.VARCHAR),
        @Result(column="phonetic", property="phonetic", jdbcType=JdbcType.LONGVARCHAR),
        @Result(column="chineseWord", property="chineseword", jdbcType=JdbcType.LONGVARCHAR),
        @Result(column="voiceFileUrl", property="voicefileurl", jdbcType=JdbcType.LONGVARCHAR),
        @Result(column="file", property="file", jdbcType=JdbcType.LONGVARCHAR)
    })
    Word selectByPrimaryKey(Long id);

    @Select({
        "select",
        "id, targetWord, addtime, addip, phonetic, chineseWord, voiceFileUrl, file",
        "from music_word"
    })
    @Results({
        @Result(column="id", property="id", jdbcType=JdbcType.BIGINT, id=true),
        @Result(column="targetWord", property="targetword", jdbcType=JdbcType.VARCHAR),
        @Result(column="addtime", property="addtime", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="addip", property="addip", jdbcType=JdbcType.VARCHAR),
        @Result(column="phonetic", property="phonetic", jdbcType=JdbcType.LONGVARCHAR),
        @Result(column="chineseWord", property="chineseword", jdbcType=JdbcType.LONGVARCHAR),
        @Result(column="voiceFileUrl", property="voicefileurl", jdbcType=JdbcType.LONGVARCHAR),
        @Result(column="file", property="file", jdbcType=JdbcType.LONGVARCHAR)
    })
    List<Word> selectAll();

    @Update({
        "update music_word",
        "set targetWord = #{targetword,jdbcType=VARCHAR},",
          "addtime = #{addtime,jdbcType=TIMESTAMP},",
          "addip = #{addip,jdbcType=VARCHAR},",
          "phonetic = #{phonetic,jdbcType=LONGVARCHAR},",
          "chineseWord = #{chineseword,jdbcType=LONGVARCHAR},",
          "voiceFileUrl = #{voicefileurl,jdbcType=LONGVARCHAR},",
          "file = #{file,jdbcType=LONGVARCHAR}",
        "where id = #{id,jdbcType=BIGINT}"
    })
    int updateByPrimaryKey(Word record);

    /**
     * 根据源单词获取单词详情
     * @param targetWord 单词名
     * @return
     */
    @Select({
            "select",
            "id, targetWord, addtime, addip, phonetic, chineseWord, voiceFileUrl, file",
            "from music_word",
            "where targetWord = #{targetWord,jdbcType=VARCHAR}"
    })
    @Results({
            @Result(column="id", property="id", jdbcType=JdbcType.BIGINT, id=true),
            @Result(column="targetWord", property="targetword", jdbcType=JdbcType.VARCHAR),
            @Result(column="addtime", property="addtime", jdbcType=JdbcType.TIMESTAMP),
            @Result(column="addip", property="addip", jdbcType=JdbcType.VARCHAR),
            @Result(column="phonetic", property="phonetic", jdbcType=JdbcType.LONGVARCHAR),
            @Result(column="chineseWord", property="chineseword", jdbcType=JdbcType.LONGVARCHAR),
            @Result(column="voiceFileUrl", property="voicefileurl", jdbcType=JdbcType.LONGVARCHAR),
            @Result(column="file", property="file", jdbcType=JdbcType.LONGVARCHAR)
    })
    Word selectByName(String targetWord);

    /**
     * 根据用户id获取用户收藏的单词信息
     * @param uid
     * @param start
     * @param count
     * @return
     */
    @Select("select " +
            "id, targetWord, addtime, addip, phonetic, chineseWord, voiceFileUrl, file, 1 isCollect " +
            "from " +
            "music_word " +
            "where " +
            "id in (select typeid from music_collect_praise where uid=#{uid} and type=4 and peration=1 and status=1) " +
            "order by targetWord limit #{start},#{count}")
    List<Map<String,Object>> selectByUid(@Param("uid") long uid, @Param("start") int start, @Param("count") int count);
}